package com.etoak.web.sys_menu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etoak.jwt.JwtUtils;
import com.etoak.utils.ResultUtils;
import com.etoak.utils.ResultVo;
import com.etoak.web.sys_menu.entity.AssignTreeParm;
import com.etoak.web.sys_menu.entity.MakeMenuTree;
import com.etoak.web.sys_menu.entity.RouterVo;
import com.etoak.web.sys_menu.entity.SysMenu;
import com.etoak.web.sys_user.entity.SysUser;
import com.etoak.web.sys_user.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.etoak.web.sys_menu.service.SysMenuService;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author by 孙有泽
 * @Date 2025/2/20 16:52
 * @Description
 */
@RestController
@RequestMapping("/api/menu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysUserService sysUserService;



    @PostMapping("/add")
    public ResultVo add(@RequestBody SysMenu sysMenu){
        if(sysMenuService.save(sysMenu)){
            return ResultUtils.success("添加成功");
        }
        return ResultUtils.error("添加失败");
    }
    @PutMapping("/update")
    public ResultVo update(@RequestBody SysMenu sysMenu){
        if(sysMenuService.updateById(sysMenu)){
            return ResultUtils.success("修改成功");
        }
        return ResultUtils.error("修改失败");
    }
    @DeleteMapping("/delete/{id}")
    public ResultVo delete(@PathVariable String id){
        QueryWrapper<SysMenu> query=new QueryWrapper<>();
        query.lambda().eq(SysMenu::getPid,id);
        List<SysMenu> list = sysMenuService.list(query);
        if(list.size()>0){
            return ResultUtils.error("该菜单下有子菜单，不能删除");
        }
        if(sysMenuService.removeById(id)){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("删除失败");
    }

    @GetMapping("/list")
    public ResultVo list(){
        QueryWrapper<SysMenu> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.lambda().orderByAsc(SysMenu::getSort);
        List<SysMenu> list = sysMenuService.list(objectQueryWrapper);
        List<SysMenu> sysMenus = MakeMenuTree.makeTree(list, "0");
        return ResultUtils.success("查询成功",sysMenus);
    }

    @GetMapping("/getParent")
    public ResultVo getParent(){
        List<SysMenu> parent = sysMenuService.getParent();
        return ResultUtils.success("查询成功",parent);

    }
    @GetMapping("/getMenuList")
    public ResultVo getMenuList(String userId){
        SysUser user = sysUserService.getById(userId);
        List<SysMenu> menuList=null;
        if(StringUtils.isNotEmpty(user.getIsadmin()+"")&& "0".equals(user.getIsadmin()+"")){
            menuList=sysMenuService.list();
        }else{
            menuList=sysMenuService.queryMenuByUserId(userId);
        }
        List<SysMenu> collect = Optional.ofNullable(menuList).orElse(new ArrayList<>()).stream().filter(x ->
               x!=null&& StringUtils.isNotEmpty(x.getType() + "") && !(x.getType() + "").equals("2")
        ).collect(Collectors.toList());

        List<RouterVo> routerVos = MakeMenuTree.makeRouterTree(collect, "0");
        return ResultUtils.success("查询成功",routerVos);

    }





}
